package com.compass.doctor.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.callback.OnTemplateItemClick
import com.compass.doctor.databinding.DialogPrescriptionRecordBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.prescription.adapter.PrescriptionRecordAdapter
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlin.math.atan

/**
 * Description： 开药记录
 * Date:2023/12/15 20:58
 */
class PrescriptionRecordDialog {
    class Builder(activity: FragmentActivity,private var  type:Int,private var patientId:String,private var onTemplateItemClick: OnTemplateItemClick?) : BaseDialogFragment.Builder<Builder>(activity),
        OnRefreshListener, OnLoadMoreListener {


        private var mPage = 1
        private val prescribeViewModel: PrescribeViewModel by activity.viewModels()
        private lateinit var recordAdapter: PrescriptionRecordAdapter


        private val mBinding: DialogPrescriptionRecordBinding =
            DialogPrescriptionRecordBinding.inflate(LayoutInflater.from(activity))

        init {

            initView()
            initData()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM
            recordAdapter = PrescriptionRecordAdapter(1)
            mBinding.refreshLayout.apply {
                setEnableRefresh(true)
                setEnableLoadMore(false)
                setOnRefreshListener(this@Builder)
                setOnLoadMoreListener(this@Builder)
            }
            mBinding.recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds(left = dpToPx(10), top = dpToPx(10), right= dpToPx(10),bottom = dpToPx(10))

                })
                adapter = recordAdapter

            }
            recordAdapter.setBottomCallBack( object : OnCustomButtonClick {
                override fun onButtonCallBack(position: Int, msgType: String?) {
                    val item=recordAdapter.getItem(position)
                    when(msgType){
                        "examine"->{
                            item?.let {
                                WebDetailActivity.start(
                                    context = activity,
                                    url = RX_DETAILS +item.id,
                                    title ="处方详情"
                                )
                            }
                            dismiss()
                        }
                        "use"->{
                            onTemplateItemClick?.onTemplateSelectCallBack(item?.id,"1")
                            dismiss()

                        }
                    }
                }
            } )

            mBinding.tvCancel.onClick {
                dismiss()
            }

        }
        /**
         * 获取开药记录
         */
        private fun getPrescriptionRecord() {
            prescribeViewModel.getHisPrescription(mPage, patientId, null, null, null,1,type)
        }
        private fun initData(){
            getPrescriptionRecord()
            prescribeViewModel.historyPrescriptionNumberLiveData.observe(activity){
                mBinding.refusalInfo.text="开药记录($it)"
            }
            prescribeViewModel.historyPrescriptionLiveData.observe(activity){
                if (mPage==1){
                    recordAdapter.setData(it)
                    mBinding.refreshLayout.finishRefresh()
                    mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                    if (it.isNullOrEmpty()) {
                        mBinding.emptyView.visible()
                    } else {
                        mBinding.emptyView.gone()
                    }
                }else{
                    mBinding.refreshLayout.finishLoadMore()
                    if (!it.isNullOrEmpty()){
                        recordAdapter.addAll(it)
                    }else{
                        mBinding.refreshLayout?.setEnableLoadMore(false)
                    }
                }
            }
        }

        override fun onRefresh(refreshLayout: RefreshLayout) {
            mPage=1
            getPrescriptionRecord()
        }

        override fun onLoadMore(refreshLayout: RefreshLayout) {
            mPage++
            getPrescriptionRecord()
        }

    }
}